<template>
	<view>
		<view class="">
			<cu-custom :isBack="true">
				<block style="color: #333333;font-size: 32rpx;" slot="content">基本信息</block>
			</cu-custom>
		</view>
		<form style="color: #777777;">
			<view class="cu-form-group">
				<view class="title">姓名</view>
				<input v-model="uname" placeholder="请输入姓名" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">性别</view>
				<picker @change="PickerSexChange" :value="sexV" :range="sex">
					<view class="input">
						{{sexV>-1?sex[sexV]:'请选择性别'}}
					</view>
				</picker>
			</view>
			<view class="cu-form-group">
				<view class="title">手机号码</view>
				<input v-model="phone" placeholder="请输入手机号号码" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">密 码</view>
				<input type="password" v-model="pwd" placeholder="请输入密码" name="password"></input>
			</view>

			<view class="padding flex flex-direction">
				<button class="cu-btn bg-red margin-tb-sm lg my-btn" @tap="save">修改</button>
			</view>
		</form>
		<message />
	</view>
</template>

<script>
	export default {
		onLoad() {
			// 请求数据
			this.getUser()
		},
		watch: {
			/* 使用watch来响应数据的变化 */
			//监听倒计时数据的变化，小于0的时候，清除定时器，解开按钮，重置倒计时
			setTimeNum(newVal, oldVal) {
				if (newVal < 0) {
					clearInterval(this.timeWrap)
					this.abledBut = false
					this.setTimeNum = 10
				}
			}
		},
		data() {
			return {
				// picker下拉数据源
				qyList: '',
				pickerData: '',
				index: -1, 
				ucId: '',
				uname: '',
				sex: ['未知', '男', '女'],
				sexV: -1,
				phone: '',
				code: '',
				pwd: '',
				address: ['伊州区', '巴里坤哈萨克自治县','伊吾县'],
				addressV: -1,
				xxdz: '',
				abledBut: false, //是否禁止
				setTimeNum: 10, // 倒计时时间
				timeWrap: null, // 定时器标识
			};
		},
		methods: {
			getUser() {
				let _this = this
				let app = getApp()
				app.globalData.request({
					url: 'wx/user/getUserInfo'
				}).then(function(res) {
					let data = res.data.data
					_this.uname = data.name
					_this.sexV = data.sex
					_this.phone = data.mobile
					_this.ucId = data.ucId
				
				}).catch(function(e) {})
			},
			getCode() {

				if (!(/^1[3456789]\d{9}$/.test(this.phone))) {
					uni.showToast({
						title: "手机号格式错误",
						icon: 'error'
					})
					return
				}
				if (this.setTimeNum > 0) {
					this.abledBut = true
					this.timeWrap = setInterval(() => {
						this.setTimeNum -= 1
					}, 1000)
				}
				let app = getApp()
				app.globalData.request({
					url: 'wx/auth/sendSms',
					data: {
						phoneNumbers: this.phone
					}
				}).then(function(res) {
				}).catch(function(e) {})
			},
			bindPickerChange: function(e, storage) {
				this.index = e.target.value
				this.pickerData = storage[this.index] // 这里就是选中的对象
			},
			PickerSexChange(e) {
				this.sexV = e.detail.value
			},
			PickerCityChange(e) {
				this.addressV = e.detail.value
			},
			PickerChange(e) {
				this.bodyV = e.detail.value
			},
			RegionChange(e) {
				this.region = e.detail.value
			},
			save() {
				let params = {
					"mobile": this.phone,
					"name": this.uname,
					"password": this.pwd,
					"sex": this.sexV,
					"ucId":this.ucId
				}

				let app = getApp()
				app.globalData.request({
					url: 'wx/user/updateInUser',
					data: params
				}).then(function(res) {
					if (res.data.code == 0) {
						uni.showToast({
							title: "用户信息修改成功"
						})
						uni.navigateBack({
							delta: 1
						});
					}
				}).catch(function(e) {})

			}
		}
	}
</script>

<style scoped>
	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}

	.my-btn {
		height: 94rpx;
		margin-top: 80px;
		background-image: linear-gradient(90deg, #FAB53A 4%, #EFA21A 99%);
		box-shadow: 0 0 12px 0 rgba(250, 181, 58, 0.50);
		border-radius: 24px;
		ont-family: PingFangSC-Medium;
		color: #FFFFFF;
		letter-spacing: 0;
		text-align: center;
	}
</style>
